package com.informatics.polymer.client.opsin;

import org.restlet.gwt.Callback;
import org.restlet.gwt.Client;
import org.restlet.gwt.data.Form;
import org.restlet.gwt.data.Protocol;
import org.restlet.gwt.data.Request;
import org.restlet.gwt.data.Response;
import org.restlet.gwt.resource.Representation;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Class implements OPSIN functionality, name to structure to conversion.
 * 
 * @author ed
 * @version 1.0
 */
public class OPSINPanel extends SectionStack {
	/**
	 * Instantiates an object of type OPSINPanel.
	 */
	public OPSINPanel() {
		setUpPanel();
	}

	/**
	 * Sets up panel layout.
	 */
	private void setUpPanel() {
		this.setVisibilityMode(VisibilityMode.MULTIPLE);
		this.setWidth(1000);
		this.setHeight(500);
		this.setBorder("2px solid #458B00");
		SectionStackSection section1 = new SectionStackSection(
				"OPSIN");
		section1.setExpanded(true);
		HTMLFlow welcome = new HTMLFlow();
		welcome.setOverflow(Overflow.AUTO);
		welcome.setPadding(10);
		String welcomeString = "Welcome to the <I>\"OPSIN\"</I> panel, please see the instructions tab "
				+ "below for more details on how to convert an IUPAC name to a Chemical Markup language structure.";
		welcome.setContents(welcomeString);
		section1.addItem(welcome);
		this.addSection(section1);
		this.setStyleName("sectionHeaderclosed");
		this.setBackgroundColor("#F0F0F0");
		//Convert name to structure panel
		final SectionStackSection convertName2Structure = new SectionStackSection("Convert Name To Structure");
		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		VLayout opsinLayout = new VLayout();
		final TextItem usernameItem = new TextItem();  
		usernameItem.setTitle("IUPAC Name"); 
		usernameItem.setRequired(true); 
		form.setFields(usernameItem);
		Button submitName = new Button("Submit");
		submitName.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				// Restlet code implemented here!
				final Client client = new Client(Protocol.HTTP);
				Form form = new Form();
				form.add("IUPACName", usernameItem.getDisplayValue());
				Representation rep = form.getWebRepresentation();
				client.post("http://localhost:8080/svc/nameToStructure", rep, new Callback() {
					@Override
					public void onEvent(Request request, Response response) {
						String resultantText = response.getEntity().getText();
						com.google.gwt.user.client.Window.alert(resultantText);
					}});
			}});
		opsinLayout.addMember(form);
		opsinLayout.addMember(submitName);
		convertName2Structure.addItem(opsinLayout);
		String cmlSourceLink = "<a href=\"http://localhost:8080/OPSINOutput.cml\">CML Source</a>";
		HTMLFlow htmlFlowPanel = new HTMLFlow();
		htmlFlowPanel.setContents(cmlSourceLink);
		convertName2Structure.addItem(htmlFlowPanel);
		this.addSection(convertName2Structure);
		// Add instructions section.
		SectionStackSection instructionSection = new SectionStackSection(
				"Instructions");
		instructionSection.setExpanded(false);
		HTMLFlow browseRepeatUnitInstructions = new HTMLFlow();
		browseRepeatUnitInstructions.setOverflow(Overflow.AUTO);
		browseRepeatUnitInstructions.setPadding(10);
		String instructions = "1. <b>Using the Generate Structure Panel.</b><br></br> Use the text box to input an IUPAC name for a chemical.<br></br>"
				+ "Press the submit button and watch a grid appear with the CML representation. A link is provided for convenient download.<br></br>"
				+ "2.<b>Delete Panel</b><br></br> If you are not happy with your selection or choose to view another panel you can click on the <I>\"Delete Panel\"</I> and press the delete panel button"
				+ " this will delete the OPSIN panel, so you can start over again.<br></br>";
		browseRepeatUnitInstructions.setContents(instructions);
		instructionSection.addItem(browseRepeatUnitInstructions);
		this.addSection(instructionSection);
		// Delete panel section
		SectionStackSection deletePanelSection = new SectionStackSection(
				"Delete Panel");
		deletePanelSection.setExpanded(false);
		Button deletePanelButton = new Button("Delete Panel!");
		deletePanelButton.setSize("100", "30");
		deletePanelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		deletePanelSection.addItem(deletePanelButton);
		this.addSection(deletePanelSection);

	}

}
